package com.xxxx.controller;


import com.xxxx.pojo.R;
import com.xxxx.pojo.Salary;
import com.xxxx.service.SalaryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chao
 * @since 2022-09-26
 */
@RestController
@RequestMapping("/salary/sob")
public class SalaryController {

    @Autowired
    private SalaryService salaryService;

    @ApiOperation(value = "获取所有工作账套")
    @GetMapping("/")
    public R getAllSalaries() {
        List<Salary> list = salaryService.list();
        return R.success(list);
    }

    @ApiOperation(value = "添加工资账套")
    @PostMapping("/")
    public R addSalary(@RequestBody Salary salary) {
        salary.setCreateDate(LocalDateTime.now());
        boolean flag = salaryService.save(salary);
        return flag ? R.success("添加成功!") : R.error("添加失败！");
    }

    @ApiOperation(value = "删除工资账套")
    @DeleteMapping("/{id}")
    public R deleteSalary(@PathVariable Integer id) {
        boolean flag = salaryService.removeById(id);
        return flag ? R.success("删除成功！") : R.error("删除失败！");
    }

    @ApiOperation(value = "更新账套")
    @PutMapping("/")
    public R updateSalary(@RequestBody Salary salary) {
        salary.setCreateDate(LocalDateTime.now());
        boolean flag = salaryService.updateById(salary);
        return flag ? R.success("更新账套成功!") : R.error("更新账套失败！");
    }
}
